//邮箱
const emailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
//手机
const mobileRegex = /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
// 密码
const password = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/

/*校验函数
参数说明:1.required必填 2.msg必填信息 3.最小值 4.最大值 5.扩展校验
*/

function getrRegex (regex, msg, extend) {
  let list = [
    {
      pattern: regex,
      message: msg,
      trigger: 'blur',
      type: 'string'
    }
  ]
  if (typeof extend === 'object') {
    extend.forEach(e => {
      list.push(e)
    })
  }
  return list
}
/*是否必填*/
function addQuired (list, required, msg) {
  if (typeof required == Boolean || required) {
    let obj = { required: true, message: msg == undefined ? '该项必填' : msg}
    list.unshift(obj)
  }
}
/*校验规则*/
let checkThis = {
  normal (required, msg, extend) {
    let list = getrRegex(null, msg, extend)
    addQuired(list, required, msg)
    return list
  },

  email (required, msg, extend) {
    let list = getrRegex(emailRegex, '请输入正确的邮箱', extend)
    addQuired(list, required, msg)
    return list
  },

  mobile (required, msg, extend) {
    let list = getrRegex(mobileRegex, '请输入正确的手机号', extend)
    addQuired(list, required, msg)
    return list
  },
  password (required, msg, extend) {
    let list = getrRegex(password, '限8~16位数字字母组合', extend)
    addQuired(list, required, msg)
    return list
  }
}

export default {
  checkThis
}
